//
//  NetworkTool.swift
//  YWT-Swift
//
//  Created by zwl on 2016/9/29.
//  Copyright © 2016年 zwl. All rights reserved.
//

import UIKit
import AFNetworking

enum MethodType {
    case GET,POST
}

class ZWLNetworkTool: AFHTTPSessionManager {
    
    
    /// 网络请求工具类的单例
    static let sharedTools: ZWLNetworkTool = {
        
        var instance = ZWLNetworkTool()
        instance.requestSerializer = AFHTTPRequestSerializer()
        instance.responseSerializer = AFHTTPResponseSerializer()
        
        return instance
    }()
    
    
    /// 字符串的拼接
    class func linkUrl(str: String) -> String {
        let path = "http://capi.douyucdn.cn/api"
        return path + str
        
    }
    
    /// 封装 GET 和 POST 请求
    ///
    /// - parameter type:            请求类型
    /// - parameter URLString:       请求的 URL
    /// - parameter parameters:      参数
    /// - parameter successCallBack: 请求成功的回调
    /// - parameter errorCallBack:   请求失败的回调
    func requestData(type: MethodType, URLString: String, parameters: [String : Any]?, successCallBack:@escaping (_ result: Any?)->(Void),errorCallBack:@escaping (_ error: Error)->(Void)) -> Void {
        
        if type == .GET {
             get(URLString, parameters: parameters, progress: nil, success: { (task, result) in
                
                    successCallBack(result)
                
                }, failure: { (task, error) in
                    
                    errorCallBack(error)
             })
        }
        
        if type == .POST {
            post(URLString, parameters: parameters, progress: nil, success: { (task, result) in
                
                    successCallBack(result)
                
                }, failure: { (task, error) in
                    
                    errorCallBack(error)
                    
            })
        }
    }

}
